package com.openbravo.pos.panels.navigator;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;

import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.user.BrowsableEditableDataModel;
import cn.ekuma.epos.db.table.erp.subject.I_Subject;

import com.openbravo.bean.erp.subject.Subject;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.JListNavigator;
import com.openbravo.data.gui.ListCellRendererBasic;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.IRenderString;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.data.user.EditorRecordAdapter;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.navigator.event.SubjectChangedListener;

public class JSubjectNavigator extends JListNavigator implements ItemListener {
	private JComboBox subjectState;
	private ComboBoxValModel m_stateModel;
	private I_DataLogic dataLogic;
	private QBFParameter filter=new QBFParameter(I_Subject.STATE);


	public JSubjectNavigator(I_DataLogic dataLogic) {
		super(new BrowsableEditableDataModel(dataLogic,Subject.class,new EditorRecordAdapter<Subject>(),new DirtyManager()));
		setCellRenderer(getListCellRenderer());
	    this.dataLogic=dataLogic;
	}
	
	private ListCellRenderer getListCellRenderer() {
		return new ListCellRendererBasic(new IRenderString<Subject>(){
			@Override
			public String getRenderString(Subject value) {	
				return value.getName();
			}
            
        });
	}
	public void loadData() throws BasicException{
		m_bd.loadData(dataLogic.queryByQBFParameter(Subject.class,filter.equal(Subject.STATE_Init)));
	}
	
	@Override
	public void valueChanged(ListSelectionEvent evt) {
		super.valueChanged(evt);
		for(SubjectChangedListener l:listenerList.getListeners(SubjectChangedListener.class)){
			l.valueSelectChanged((Subject) m_bd.getCurrentElement());
		}
	}
	
	public void addSubjectChangedListener(SubjectChangedListener l){
		listenerList.add(SubjectChangedListener.class, l);
	}
	
	public void removeSubjectChangedListener(SubjectChangedListener l){
		listenerList.remove(SubjectChangedListener.class, l);
	}

	@Override
	public JPanel getNorthPanel() {
		JPanel panel=new JPanel();
		panel.setLayout(new BorderLayout(0, 0));
		subjectState = new JComboBox();
		panel.add(subjectState,BorderLayout.CENTER);
		m_stateModel=new ComboBoxValModel();
		m_stateModel.add(new ComboBoxValEntry(Subject.STATE_Init,AppLocal.getIntString("list.Subject.STATE_Init")));
		m_stateModel.add(new ComboBoxValEntry(Subject.STATE_inProcess,AppLocal.getIntString("list.STATE_inProcess")));
		m_stateModel.add(new ComboBoxValEntry(Subject.STATE_Complete,AppLocal.getIntString("list.STATE_Complete")));
		m_stateModel.add(new ComboBoxValEntry(Subject.STATE_Cancel,AppLocal.getIntString("list.Subject.STATE_Cancel")));
		subjectState.setModel(m_stateModel);
		subjectState.addItemListener(this);
		return panel;
	}


	@Override
	public void itemStateChanged(ItemEvent e) {
		try {
			m_bd.loadData(dataLogic.queryByQBFParameter(Subject.class,filter.equal(m_stateModel.getSelectedKey())));
		} catch (BasicException e1) {
			e1.printStackTrace();
			new MessageInf(e1).show(this);
		}
	}
	
}
